package com.tdk.wifidirectserver.ui.chat

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.LinearLayoutManager
import com.tdk.wifidirectserver.adapter.MessageAdapter
import com.tdk.wifidirectserver.databinding.FragmentChatBinding
import com.tdk.wifidirectserver.ui.main.ConnectedClientViewModel
import com.tdk.wifidirectserver.ui.main.ConnectedServerViewModel

class ChatFragment : Fragment() {
    private lateinit var messageAdapter: MessageAdapter
    lateinit var binding: FragmentChatBinding
    var mIsServer = false
    var mIp = ""
    var viewModel: ViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentChatBinding.inflate(inflater, container, false)
        initView()
        return binding.root
    }

    private fun initView() {
        if (mIsServer) {
            val connectedServerViewModel = viewModel as ConnectedServerViewModel
            messageAdapter = MessageAdapter(connectedServerViewModel.messageList)
            binding.messageRv.adapter = messageAdapter
            binding.messageRv.layoutManager =
                LinearLayoutManager(this.requireContext(), LinearLayoutManager.VERTICAL, false)
            connectedServerViewModel.messageListUpdateLiveData.observe(viewLifecycleOwner) {
                messageAdapter.notifyItemChanged(it - 1)
                binding.messageRv.scrollToPosition(it - 1)
            }
            binding.btSend.setOnClickListener {
                connectedServerViewModel.sengMsg(binding.etInput.text.toString())
                binding.etInput.text.clear()
            }
        } else {
            val connectedClientViewModel = viewModel as ConnectedClientViewModel
            messageAdapter = MessageAdapter(connectedClientViewModel.messageList)
            binding.messageRv.adapter = messageAdapter
            binding.messageRv.layoutManager =
                LinearLayoutManager(this.requireContext(), LinearLayoutManager.VERTICAL, false)
            connectedClientViewModel.messageListUpdateLiveData.observe(viewLifecycleOwner) {
                messageAdapter.notifyItemChanged(it - 1)
                binding.messageRv.scrollToPosition(it - 1)
            }
            binding.btSend.setOnClickListener {
                connectedClientViewModel.sengMsg(binding.etInput.text.toString())
                binding.etInput.text.clear()
            }
        }


    }

    companion object {
        @JvmStatic
        fun newInstance(isServer: Boolean, ip: String, viewModel: ViewModel) =
            ChatFragment().apply {
                mIsServer = isServer
                mIp = ip
                this.viewModel = viewModel
            }
    }
}